package engine.game;

import java.io.Serializable;
import java.util.ArrayList;

import engine.graphics.IRender;
import engine.objects.GameObjects;
import engine.objects.PhysicsObject;
import engine.system.IInit;

public abstract class Manager  implements IInit, IRender, Serializable {
	
	private static final long serialVersionUID = 1L;
	
	private GameObjects gameObjects ; 
	private ArrayList<PhysicsObject> objects; 
	private Level level ; 
	
	public Manager(GameObjects go, Level lvl){
		setGameObjects(go); 
		this.setLevel(lvl);
	}

	public void init(){
		this.objects = new ArrayList<PhysicsObject>();
	}
	
	public GameObjects getGameObjects() {
		return gameObjects;
	}

	private void setGameObjects(GameObjects gameObjects) {
		this.gameObjects = gameObjects;
	}
	
	public Level getLevel(){
		return this.level ; 
	}
	
	private void setLevel(Level lvl){
		this.level = lvl ; 
	}
	
	public ArrayList<PhysicsObject> getObjects(){
		return this.objects ; 
	}
	
	public void remove(PhysicsObject o){
		if(o != null){
			this.getGameObjects().getObjects().remove(o); 
			this.objects.remove(o); 
		}
	}
	
	public void addNewObject(PhysicsObject o) {
		this.objects.add(o); 
		this.getLevel().gameObjects.addNewObject(o);
	}
	
	@Override
	public void update() {
		try{
			for(PhysicsObject i: objects){
					i.update(); 
			}
		}catch(Exception e){
//			e.printStackTrace(); 
		}
	}

	@Override
	public void render() {
		for(PhysicsObject i: objects)
			i.render(); 
	}
	
}
